package cn.neud.trace.note.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import cn.neud.trace.note.service.NoteService;
import cn.neud.trace.note.service.FollowService;
import cn.neud.trace.note.service.UserService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.neud.trace.note.model.dto.Result;
import cn.neud.trace.note.model.dto.ScrollResult;
import cn.neud.trace.note.model.dto.UserDTO;
import cn.neud.trace.note.model.entity.Note;
import cn.neud.trace.note.model.entity.Follow;
import cn.neud.trace.note.model.entity.User;
import cn.neud.trace.note.mapper.NoteMapper;
import cn.neud.trace.note.constant.SystemConstants;
import cn.neud.trace.note.utils.UserLocal;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static cn.neud.trace.note.constant.RedisConstant.NOTE_LIKED_KEY;
import static cn.neud.trace.note.constant.RedisConstant.FEED_KEY;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author David
 * @since 2021-12-22
 */
@Service
public class NoteServiceImpl extends ServiceImpl<NoteMapper, Note> implements NoteService {

    @Resource
    private UserService userService;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Resource
    private FollowService followService;

    @Override
    public Result queryHotNote(Integer current) {
        // 根据用户查询
        Page<Note> page = query()
                .orderByDesc("liked")
                .page(new Page<>(current, SystemConstants.MAX_PAGE_SIZE));
        // 获取当前页数据
        List<Note> records = page.getRecords();
        // 查询用户
        records.forEach(note -> {
            this.queryNoteUser(note);
            this.isNoteLiked(note);
        });
        return Result.ok(records);
    }

    @Override
    public Result queryNoteById(Long id) {
        // 1.查询note
        Note note = getById(id);
        if (note == null) {
            return Result.fail("笔记不存在！");
        }
        // 2.查询note有关的用户
        queryNoteUser(note);
        // 3.查询note是否被点赞
        isNoteLiked(note);
        return Result.ok(note);
    }

    private void isNoteLiked(Note note) {
        // 1.获取登录用户
        UserDTO user = UserLocal.getUser();
        if (user == null) {
            // 用户未登录，无需查询是否点赞
            return;
        }
        Long userId = user.getId();
        // 2.判断当前登录用户是否已经点赞
        String key = "note:liked:" + note.getId();
        Double score = stringRedisTemplate.opsForZSet().score(key, userId.toString());
        note.setIsLike(score != null);
    }

    @Override
    public Result likeNote(Long id) {
        // 1.获取登录用户
        Long userId = UserLocal.getUser().getId();
        // 2.判断当前登录用户是否已经点赞
        String key = NOTE_LIKED_KEY + id;
        Double score = stringRedisTemplate.opsForZSet().score(key, userId.toString());
        if (score == null) {
            // 3.如果未点赞，可以点赞
            // 3.1.数据库点赞数 + 1
            boolean isSuccess = update().setSql("liked = liked + 1").eq("id", id).update();
            // 3.2.保存用户到Redis的set集合  zadd key value score
            if (isSuccess) {
                stringRedisTemplate.opsForZSet().add(key, userId.toString(), System.currentTimeMillis());
            }
        } else {
            // 4.如果已点赞，取消点赞
            // 4.1.数据库点赞数 -1
            boolean isSuccess = update().setSql("liked = liked - 1").eq("id", id).update();
            // 4.2.把用户从Redis的set集合移除
            if (isSuccess) {
                stringRedisTemplate.opsForZSet().remove(key, userId.toString());
            }
        }
        return Result.ok();
    }

    @Override
    public Result queryNoteLikes(Long id) {
        String key = NOTE_LIKED_KEY + id;
        // 1.查询top5的点赞用户 zrange key 0 4
        Set<String> top5 = stringRedisTemplate.opsForZSet().range(key, 0, 4);
        if (top5 == null || top5.isEmpty()) {
            return Result.ok(Collections.emptyList());
        }
        // 2.解析出其中的用户id
        List<Long> ids = top5.stream().map(Long::valueOf).collect(Collectors.toList());
        String idStr = StrUtil.join(",", ids);
        // 3.根据用户id查询用户 WHERE id IN ( 5 , 1 ) ORDER BY FIELD(id, 5, 1)
        List<UserDTO> userDTOS = userService.query()
                .in("id", ids).last("ORDER BY FIELD(id," + idStr + ")").list()
                .stream()
                .map(user -> BeanUtil.copyProperties(user, UserDTO.class))
                .collect(Collectors.toList());
        // 4.返回
        return Result.ok(userDTOS);
    }

    @Override
    public Result saveNote(Note note) {
        // 1.获取登录用户
        UserDTO user = UserLocal.getUser();
        note.setUserId(user.getId());
        // 2.保存留言笔记
        boolean isSuccess = save(note);
        if (!isSuccess) {
            return Result.fail("新增笔记失败!");
        }
        // 3.查询笔记作者的所有粉丝 select * from tb_follow where follow_user_id = ?
        List<Follow> follows = followService.query().eq("follow_user_id", user.getId()).list();
        // 4.推送笔记id给所有粉丝
        for (Follow follow : follows) {
            // 4.1.获取粉丝id
            Long userId = follow.getUserId();
            // 4.2.推送
            String key = FEED_KEY + userId;
            stringRedisTemplate.opsForZSet().add(key, note.getId().toString(), System.currentTimeMillis());
        }
        // 5.返回id
        return Result.ok(note.getId());
    }

    @Override
    public Result queryNoteOfFollow(Long max, Integer offset) {
        // 1.获取当前用户
        Long userId = UserLocal.getUser().getId();
        // 2.查询收件箱 ZREVRANGEBYSCORE key Max Min LIMIT offset count
        String key = FEED_KEY + userId;
        Set<ZSetOperations.TypedTuple<String>> typedTuples = stringRedisTemplate.opsForZSet()
                .reverseRangeByScoreWithScores(key, 0, max, offset, 2);
        // 3.非空判断
        if (typedTuples == null || typedTuples.isEmpty()) {
            return Result.ok();
        }
        // 4.解析数据：noteId、minTime（时间戳）、offset
        List<Long> ids = new ArrayList<>(typedTuples.size());
        long minTime = 0; // 2
        int os = 1; // 2
        for (ZSetOperations.TypedTuple<String> tuple : typedTuples) { // 5 4 4 2 2
            // 4.1.获取id
            ids.add(Long.valueOf(tuple.getValue()));
            // 4.2.获取分数(时间戳）
            long time = tuple.getScore().longValue();
            if (time == minTime) {
                os++;
            } else {
                minTime = time;
                os = 1;
            }
        }

        // 5.根据id查询note
        String idStr = StrUtil.join(",", ids);
        List<Note> notes = query().in("id", ids).last("ORDER BY FIELD(id," + idStr + ")").list();

        for (Note note : notes) {
            // 5.1.查询note有关的用户
            queryNoteUser(note);
            // 5.2.查询note是否被点赞
            isNoteLiked(note);
        }

        // 6.封装并返回
        ScrollResult r = new ScrollResult();
        r.setList(notes);
        r.setOffset(os);
        r.setMinTime(minTime);

        return Result.ok(r);
    }

    private void queryNoteUser(Note note) {
        Long userId = note.getUserId();
        User user = userService.getById(userId);
        note.setName(user.getNickName());
        note.setIcon(user.getIcon());
    }
}
